// pages/index_career/index.js
const apis = require('../../../utils/api').apis;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sortWayArr: [{
        name: '综合排序',
        eName: ''
      },
      {
        name: '附近',
        eName: 'near'
      },
      {
        name: '最新',
        eName: 'new'
      }
    ],
    sortWayActive: 0,
    activeIndex: -1,
    filterArr: [],
    dataArr: [],
    industryArr: [],
    industryActive: [], //行业领域选中
    experienceList: '', //经验要求选中
    salaryList: '', //薪水要求选中
    sortWay: '', //排序方式
    salaryActive:0,
    experienceActive:0,
    pageSize:10,
    pageNo:1,
    total:0,
    show:false,
    searchResultArr:[],
    searchPageSize:10,
    searchPageNo:1,
    searchTotal:0,
    searchWord:""
  },
  changInd(e) {
    // console.log(e.currentTarget.dataset.ind);
    if (this.data.activeIndex == -1) {
      this.setData({
        activeIndex: e.currentTarget.dataset.ind
      })
    } else {
      this.setData({
        activeIndex: -1
      })
    }
  },
  confirmValue(e){
    let _this=this;
    let val=e.detail.val;
    this.setData({
      searchWord:val
    })
    // console.log('confirm接收',val)
    if(val!==''){
      this.getSearchData()
    }else{
      _this.setData({
        show: false
      })
    }
    
  },
  FoucsValue(e){
    let val=e.detail.val;
    // console.log('foucs接收',val)
    if(val==''){
      this.setData({
        show:false
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    apis.recruitmentQueryDictionary({}).then(res => {
      this.setData({
        filterArr: res.data,
        experienceList: res.data.experienceList[0],
        salaryList: res.data.salaryList[0]
      })
      this.getData()
    })
    apis.industryQueryAllIndustryApi().then(res => {
      let arr = res.data
      arr.map(item => {
        item.select = 0
      })
      this.setData({
        industryArr: arr,
      })
      // console.log(this.data.industryArr)
    })


  },
  getData() {
    wx.showLoading({
      title: '加载中',
    })
    apis.recruitmentQueryRecruitment({
      industryId: this.data.industryActive,
      experienceRequirement: this.data.experienceList,
      salaryRange: this.data.salaryList,
      sortWay: this.data.sortWay,
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize  
    }).then(res => {
      if(this.data.pageNo==1){
        this.setData({
          dataArr: res.data.records,
          total:res.data.total
        })
      }else{
        this.setData({
          dataArr: this.data.dataArr.concat(res.data.records),
          total:res.data.total
        })
      }
      // console.log(this.data.pageNo,this.data.dataArr)
      wx.hideLoading();
    })
  },
  getSearchData(){
    let _this=this;
    apis.searchSearchRecruitment({
      searchWhere: _this.data.searchWord,
      pageNo:_this.data.searchPageNo,
      pageSize:_this.data.searchPageSize
    }).then(res => {
      // console.log(res)
      if(_this.data.searchPageNo==1){
        _this.setData({
          searchResultArr: res.data.records,
          searchTotal:res.data.total,
          show: true
        })
      }else{
        _this.setData({
          searchResultArr: _this.data.searchResultArr.concat(res.data.records),
          searchTotal:res.data.total,
          show: true
        })
      }
      // console.log(_this.data.searchPageNo,_this.data.searchResultArr)
    })
  },
  // 综合排序
  sortWay(e){
    this.setData({
      sortWayActive:e.currentTarget.dataset.index,
      sortWay:e.currentTarget.dataset.type,
      activeIndex: -1,
      pageNo:1,
    })
    this.getData()
  },
  // 行业领域筛选
  industryTap(e) {
    let id = e.currentTarget.dataset.id;
    let arr = this.data.industryArr
    arr.map(item => {
      if (id == item.id) {
        if(item.select == 1){
          item.select = 0;
          this.setData({
            industryArr: arr
          })
        }else{
          item.select = 1;
        this.setData({
          industryArr: arr
        })
        }
        
      }
    })
  },
  clearIndustry() {
    let arr = this.data.industryArr;
    arr.map(item => {
      item.select = 0
    })
    this.setData({
      industryArr: arr
    })
  },
  sureIndustry() {
    let sureArr = []
    let arr = this.data.industryArr;
    arr.map(item => {
      if (item.select == 1) {
        sureArr.push(item.id)
      }
    })
    this.setData({
      activeIndex: -1,
      industryActive: sureArr,
      pageNo:1,
    })
    this.getData()

  },
  // 更多筛选
  salary(e){
    this.setData({
      salaryActive:e.currentTarget.dataset.index,
      salaryList:e.currentTarget.dataset.type,
    })
  },
  experience(e){
    this.setData({
      experienceActive:e.currentTarget.dataset.index,
      experienceList:e.currentTarget.dataset.type,
    })
  },
  sureMore(){
    this.setData({
      activeIndex:-1,
      pageNo:1,
    })
    this.getData()
  },
  clearMore(){
    this.setData({
      salaryActive:-1,
    experienceActive:-1,
    experienceList:'',
    salaryList:''
    })
  },
  goDetail(e){
    let id=e.currentTarget.dataset.id;
    let industryId=e.currentTarget.dataset.industryid;
    wx.navigateTo({
      url: '../index_career_detail/index?id='+id+'&industryId='+industryId,
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.show,this.data.searchTotal,this.data.searchResultArr)
    if(!this.data.show&&this.data.total>this.data.dataArr.length){
      this.setData({
        pageNo: ++this.data.pageNo
      })
      this.getData()
    }else if(this.data.show&&this.data.searchTotal>this.data.searchResultArr.length){
      this.setData({
        searchPageNo: ++this.data.searchPageNo
      })
      this.getSearchData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})